package com.zlq.cn.service.un;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlq.cn.api.ResponseException;
import com.zlq.cn.dto.DataCountDTO;
import com.zlq.cn.dto.DateCountDTO;
import com.zlq.cn.dto.VipLogTotalDTO;
import com.zlq.cn.dto.VipLogTotalGroupDTO;
import com.zlq.cn.entity.User;
import com.zlq.cn.entity.VipLog;
import com.zlq.cn.enums.VipLevelEnum;
import com.zlq.cn.mapper.un.VipLogMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhoucl
 * @since 2020-06-13
 */
@Service
public class VipLogService extends ServiceImpl<VipLogMapper, VipLog> {
    @Resource
    private UserService userService;
    @Resource
    private IosService iosService;

    public synchronized void saveUserVipReport(String orderType, String orderNo, String receipt, User loginUser) throws Exception {
        VipLog one = lambdaQuery().eq(VipLog::getOrderNo, orderNo).one();
        if (one != null) {
            ResponseException.resultApiError("订单号异常");
        }
        if (iosService.buyAppVerify(orderNo, receipt, null)) {
            VipLog vipLog = new VipLog();
            vipLog.setOrderType(orderType);
            vipLog.setOrderNo(orderNo);
            vipLog.setReceipt(receipt);
            vipLog.setUserId(loginUser.getId());
            vipLog.setCreateTime(LocalDateTime.now());
            vipLog.setUpdateTime(LocalDateTime.now());
            save(vipLog);
            userService.updateVipTime(orderType, loginUser);
        } else {
            ResponseException.resultApiError("订单号异常");
        }
    }

    public List<DataCountDTO> totalCount(DateCountDTO dateCountDTO) {
        List<DataCountDTO> dataCountDTOS = new ArrayList<>();
        long days = Math.abs(dateCountDTO.getEndTime().until(dateCountDTO.getStartTime(), ChronoUnit.DAYS));
        for (long i = 0; i < days; i++) {
            DataCountDTO dataCountDTO = new DataCountDTO();
            dataCountDTO.setCountField(dateCountDTO.getEndTime().minusDays(i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            dataCountDTO.setCountResult(0);
            dataCountDTOS.add(dataCountDTO);
        }
        List<DataCountDTO> queryList = baseMapper.totalCount(dateCountDTO);
        int total = 0;
        for (DataCountDTO dataCountDTO : dataCountDTOS) {
            if (CollectionUtils.isNotEmpty(queryList)) {
                for (DataCountDTO countDTO : queryList) {
                    if (countDTO.getCountField().equals(dataCountDTO.getCountField())) {
                        dataCountDTO.setCountResult(countDTO.getCountResult());
                        break;
                    }
                }
            }
            total += dataCountDTO.getCountResult();
        }
        DataCountDTO dataCountDTO = new DataCountDTO();
        dataCountDTO.setCountField("近7天总计");
        dataCountDTO.setCountResult(total);
        dataCountDTOS.add(0, dataCountDTO);
        return dataCountDTOS;
    }

    public VipLogTotalDTO vipLogTotal(LocalDateTime startTime, LocalDateTime endTime) {
        VipLogTotalDTO vipLogTotalDTO = ObjectUtils.defaultIfNull(baseMapper.vipTotalCount(), new VipLogTotalDTO());
        VipLogTotalDTO vipLogTotalDTODate = ObjectUtils.defaultIfNull(baseMapper.vipTotalCountDate(startTime, endTime), new VipLogTotalDTO());
        vipLogTotalDTO.setDateTotal(vipLogTotalDTODate.getDateTotal());
        vipLogTotalDTO.setDateTotalPrice(vipLogTotalDTODate.getDateTotalPrice());
        List<VipLogTotalGroupDTO> vipLogTotalGroupDTOS = baseMapper.vipTotalCountGroup(startTime, endTime);
        if (CollectionUtils.isNotEmpty(vipLogTotalGroupDTOS)) {
            for (VipLogTotalGroupDTO vipLogTotalGroupDTO : vipLogTotalGroupDTOS) {
                if (VipLevelEnum.ZHOU.getType().equals(vipLogTotalGroupDTO.getOrderType())) {
                    vipLogTotalDTO.setZhouTotal(vipLogTotalGroupDTO.getTotal());
                    vipLogTotalDTO.setZhouTotalPrice(vipLogTotalGroupDTO.getTotalPrice());
                } else if (VipLevelEnum.YUE.getType().equals(vipLogTotalGroupDTO.getOrderType())) {
                    vipLogTotalDTO.setYueTotal(vipLogTotalGroupDTO.getTotal());
                    vipLogTotalDTO.setYueTotalPrice(vipLogTotalGroupDTO.getTotalPrice());
                } else if (VipLevelEnum.JI.getType().equals(vipLogTotalGroupDTO.getOrderType())) {
                    vipLogTotalDTO.setJiTotal(vipLogTotalGroupDTO.getTotal());
                    vipLogTotalDTO.setJiTotalPrice(vipLogTotalGroupDTO.getTotalPrice());
                } else if (VipLevelEnum.NIAN.getType().equals(vipLogTotalGroupDTO.getOrderType())) {
                    vipLogTotalDTO.setNianTotal(vipLogTotalGroupDTO.getTotal());
                    vipLogTotalDTO.setNianTotalPrice(vipLogTotalGroupDTO.getTotalPrice());
                }
            }
        }
        return vipLogTotalDTO;
    }

    public List<User> vipLogDetails(Page<User> page, String orderType, LocalDateTime startTime, LocalDateTime endTime) {
        return baseMapper.vipLogDetails(page, orderType, startTime, endTime);
    }
}
